package samsung.yx;

import java.util.Scanner;

public class Poj3009 {

	static int n, m, flag, ans, sx, sy;
	static int map[][] = new int[50][50];
	static int vis[][] = new int[50][50];

	public static void main(String[] args) throws Exception {
		/*Scanner sc = new Scanner(new FileInputStream(
				"C:\\Users\\tstcit\\Desktop\\samsung_yx\\Poj3009.txt"));*/
		Scanner sc = new Scanner(System.in);
		int i, j;
		while (sc.hasNext()) {
			m = sc.nextInt();
			n = sc.nextInt();
			if (m == 0 && n == 0) {
				break;
			}
			ans = 0xFFFFFFF;
			flag = 0;
			init();
			for (i = 1; i <= n; i++) {
				for (j = 1; j <= m; j++) {
					map[i][j] = sc.nextInt();
					if (map[i][j] == 2) {
						sx = i;
						sy = j;
					}
				}
			}
			dfs(sx, sy, 0, 0);
			if (flag > 0) {
				if (ans <= 10) {
					System.out.println(ans);
				} else {
					System.out.println("-1");
				}
			} else {
				System.out.println("-1");
			}
		}
	}

	public static void init() {
		for(int i=0;i<50;i++){
			for(int j=0;j<50;j++){
				map[i][j]=-1;
			}
		}
	}

	public static void dfs(int x, int y, int w, int step) {
		if (step > 10 || step >= ans) {
			return;
		}
		if (map[x][y] == 0 || map[x][y] == 3 || map[x][y] == 2) {
			while (true) {
				if (map[x][y] == 2) {
					map[x][y] = 0;
					break;
				}
				if (map[x][y] == 1) {
					map[x][y] = 0;
					if (w == 1) {
						x++;
					} else if (w == 2) {
						y--;
					} else if (w == 3) {
						x--;
					} else if (w == 4) {
						y++;
					}
					break;
				}
				if (map[x][y] == -1) {
					return;
				}
				if (map[x][y] == 3) {
					flag = 1;
					if (ans > step) {
						ans = step;
						return;
					}
				}
				if (w == 1) {
					x--;
				} else if (w == 2) {
					y++;
				} else if (w == 3) {
					x++;
				} else if (w == 4) {
					y--;
				}
			}
		}
		if (map[x - 1][y] != 1 && map[x - 1][y] != -1) {
			dfs(x - 1, y, 1, step + 1);
		}
		if (map[x][y + 1] != 1 && map[x][y + 1] != -1) {
			dfs(x, y + 1, 2, step + 1);
		}
		if (map[x + 1][y] != 1 && map[x + 1][y] != -1) {
			dfs(x + 1, y, 3, step + 1);
		}
		if (map[x][y - 1] != 1 && map[x][y - 1] != -1) {
			dfs(x, y - 1, 4, step + 1);
		}
		if (w == 1) {
			map[x - 1][y] = 1;
		} else if (w == 2) {
			map[x][y + 1] = 1;
		} else if (w == 3) {
			map[x + 1][y] = 1;
		} else if (w == 4) {
			map[x][y - 1] = 1;
		}
		step--;
	}
}
